package cn.bdqn.activity02;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class OneActivity extends AppCompatActivity implements View.OnClickListener {
   private Button btnTwo,btnThree;
    private TextView tv_one;
 private static final int rq_two=2,rq_three=3;//传数据一般用常量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        btnTwo=findViewById(R.id.btn_two);
        btnThree=findViewById(R.id.btn_three);
        btnTwo.setOnClickListener(this);
        btnThree.setOnClickListener(this);
        tv_one=findViewById(R.id.tv_one);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_two:
                Intent intent=new Intent(OneActivity.this,TwoActivity.class);
                intent.putExtra("msg","A告诉B");
                startActivityForResult(intent,rq_two);
                break;
            case R.id.btn_three:
                Intent intent2=new Intent(OneActivity.this,ThreeActivity.class);
                intent2.putExtra("msg","A告诉C");
               startActivityForResult(intent2,rq_three);
                break;
            default: break;
        }
    }

    //requestCode请求码
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Intent intent=getIntent();
        //根据状态码进行判断
        switch (requestCode){
            case rq_two:
                Toast.makeText(this, intent.getStringExtra("twomsg"), Toast.LENGTH_SHORT).show();
                break;
            case rq_three:
                Toast.makeText(this, intent.getStringExtra("threemsg"), Toast.LENGTH_SHORT).show();
                break;
            default:break;
        }

    }
}
